package yangmengyao.experiment12.service;

import org.springframework.stereotype.Service;
import yangmengyao.experiment12.model.Student;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {
    private final List<Student> students = new ArrayList<>();

    @PostConstruct
    public void init() {
        // 初始化一些学生数据
        Student student1 = new Student();
        student1.setId(1L);
        student1.setFirstName("John");
        student1.setLastName("Doe");
        student1.setEmail("john.doe@example.com");
        student1.setAge(20);
        students.add(student1);

        Student student2 = new Student();
        student2.setId(2L);
        student2.setFirstName("Jane");
        student2.setLastName("Doe");
        student2.setEmail("jane.doe@example.com");
        student2.setAge(22);
        students.add(student2);
    }

    public List<Student> findAllStudents() {
        return students; // 返回一个副本以防止修改内部列表
    }

    public Student createStudent(Student student) {
        if (student.getId() == null) {
            student.setId(System.currentTimeMillis()); // 简单的ID生成策略
        }
        students.add(student);
        return student;
    }
}